package com.ben.notebook.controller;

import com.ben.notebook.domain.Note;
import com.ben.notebook.domain.NoteStatusEnum;
import com.ben.notebook.infrastructure.mapper.NoteMapper;
import com.ben.notebook.infrastructure.util.JsonResult;
import com.ben.notebook.infrastructure.util.JsonUtil;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * cqrs命令与查询职责分离
 * 查询则是直接来自mybatis，只是单纯获取数据
 */
@RestController
@RequestMapping("note")
public class NoteQueryApi {
    //后续改为redis接口，即所有读操作不直接调用数据库，数据查询实现应该对接口隐藏
    private final NoteMapper mapper;

    public NoteQueryApi(NoteMapper mapper) {
        this.mapper = mapper;
    }

    /**
     * 查询所有有效数据
     */
    @GetMapping("/all")
    public JsonResult getAll(){
        List<Note> all = mapper.getAllWithoutDel(NoteStatusEnum.DELETED.getValue());
        return JsonResult.ok(all);
    }

    @PostMapping("/allbyobj")
    public JsonResult getNoteListByObj(@RequestBody String noteJson) throws IOException {
        Note note = JsonUtil.jsonStr2Obj(noteJson, Note.class);
        //值为空的属性过滤掉
        List<Note> notes = mapper.getFilterList(note, NoteStatusEnum.DELETED.getValue());
        return JsonResult.ok(notes);
    }

}
